<?php
  include("DBConfig.class.php");

  class ProvinceChart{

    public static function getPovertyRatioChartByCode($code){
      //Query Province  $votename, $country, $filter
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  pro_id, 
                  pro_name,
                  pro_code,
                  pro_revenue,
                  pro_ratio_poverty,
                  pro_lat,
                  pro_lon
                FROM ".DBConfig::$dbname.".province
                WHERE pro_code = :code
                GROUP BY pro_id
                ORDER BY pro_id ASC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        //var_dump($result);
        ProvinceChart::renderPovertyRatioChart($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
      }
    }


    public static function renderPovertyRatioChart($result){
      include("gchart/provincePovertyRatioChart.php");
    }



    public static function getPovertyGapChartByCode($code){
      //Query Province  $votename, $country, $filter
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  pro_id, 
                  pro_name,
                  pro_code,
                  pro_revenue,
                  pro_gap_poverty,
                  pro_lat,
                  pro_lon
                FROM ".DBConfig::$dbname.".province
                WHERE pro_code = :code
                GROUP BY pro_id
                ORDER BY pro_id ASC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        //var_dump($result);
        ProvinceChart::renderPovertyGapChart($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
      }
    }


    public static function renderPovertyGapChart($result){
      include("gchart/provincePovertyGapChart.php");
    }




    public static function getPovertyViolenceChartByCode($code){
      //Query Province  $votename, $country, $filter
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  pro_id, 
                  pro_name,
                  pro_code,
                  pro_revenue,
                  pro_violence_poverty,
                  pro_lat,
                  pro_lon
                FROM ".DBConfig::$dbname.".province
                WHERE pro_code = :code
                GROUP BY pro_id
                ORDER BY pro_id ASC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        //var_dump($result);
        ProvinceChart::renderPovertyViolenceChart($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
      }
    }


    public static function renderPovertyViolenceChart($result){
      include("gchart/provincePovertyViolenceChart.php");
    }


  }
?>